package com.baicaoyuan.web.controller;


import com.baicaoyuan.web.common.LogUtil;
import com.baicaoyuan.web.common.Status;
import com.baicaoyuan.web.common.annotation.MemberAccess;
import com.baicaoyuan.web.common.exception.StateMessage;
import com.baicaoyuan.web.request.br.BNoticeIdRequest;
import com.baicaoyuan.web.request.br.BNoticePageRequest;
import com.baicaoyuan.web.request.br.BNoticeRequest;
import com.baicaoyuan.web.response.NoticeResponse;
import com.baicaoyuan.web.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Statement;
import java.util.List;

@Api(value = "通知Controller", tags = {"通知操作接口"})
@RestController
@RequestMapping(value = "/noice")
public class NoticeController {


    @Autowired
    NoticeService noticeService;

    @ApiOperation(value = "后台-发布通知（token）", notes = "", code = 200, produces = "application/json", response = Status.class)
    @RequestMapping(value = "/saveNotice", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status saveNotice(@RequestBody BNoticeRequest request) {
        try {
            noticeService.save(request);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-编辑通知（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/editNotice", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status editNotice(@RequestBody BNoticeRequest request) {
        try {
            noticeService.edit(request);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-删除通知（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/deleteNotice", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status delete(@RequestBody BNoticeIdRequest request) {
        try {
            this.noticeService.delete(request.getId());
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-(前台)-获取通知列表", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getNotices", method = RequestMethod.POST)
    @ResponseBody
    public Status getNotices(@RequestBody BNoticePageRequest request) {
        try {
            List<NoticeResponse> user = this.noticeService.getNotices(request.getPage(), request.getSize());
            Integer count = this.noticeService.getCountOfAll();
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("notices", user);
            result.setData("totalCount", count);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }


}
